<div class="posts index form">
<h2><?php __('Posts');?></h2>
<?php //echo $this->element('form', array('plugin' => 'searchable')); ?>
<!-- Search Box -->
<p class="searcher">
    <?php e($form->input('Post.search',
        array('class'=>'input text', 'label'=>false))); ?>
    <?php e($html->link(__('Search Posts', true),
        array('controller'=>'posts', 'action'=>'index', 'title'=>''),
        array('class'=>'button submit', 'style'=>'clear:none', 'id'=>'link_search'))); ?>
</p>
<br class="clear"/>
<?php if(count($posts)): ?>
    <!-- Table Navigation -->
    <div class="tablenav">
        <div class="batchactions"></div>
        <?php echo $this->element('pager'); ?>
    </div>
    <!-- Table Content Area -->
    <table id="table_posts" class="table fixed">
        <thead title="<?php __('Click a column to sort data table'); ?>">
            <tr>
                <th  width="5%" class="checkbox" title="<?php __('Click to select/deselect all records'); ?>"><input type="checkbox" class="checkbox"/></th>
                <th width="43%"><?php __('Post Title');?></th>
                <th width="14%"><?php __('PostTerm');?></th>
                <th width="14%"><?php __('Comment'); ?></th>
                <th width="25%"><?php __('Extra'); ?></th>
            </tr>
        </thead>
        <tfoot title="<?php __('Click a column to sort data table'); ?>">
            <tr>
                <th class="checkbox" title="<?php __('Click to select/deselect all records'); ?>"><input type="checkbox" class="checkbox"/></th>
                <th><?php __('Post Title');?></th>
                <th><?php __('PostTerm');?></th>
                <th><?php __('Comment'); ?></th>
                <th><?php __('Extra'); ?></th>
            </tr>
        </tfoot>
        <tbody>
            <?php foreach ($posts as $i=>$post):
                $class = $i++ % 2 == 0 ? ' class="altrow"' : ''; ?>
            <tr<?php e($class);?>>
                <td class="checkbox"><input type="checkbox" class="checkbox"/></td>
                <!-- Post.title, Post.comment, Post.attachment -->
                <td class="title">
                    <p class="title">
                        <!-- Post.title -->
                        <?php e($post['Post']['title']); ?>
                        <!-- Post.Attachment -->
                        <?php if($post['Post']['attachment_count']): ?>
                            (<?php e($html->link('', 'javascript:void(0)',
                                array(
                                    'title'=>sprintf(__('%d attachments in total', true), $post['Post']['attachment_count']),
                                    'class'=>'has_attachment'
                            ))); ?>)
                        <?php endif; ?>
                        <!-- External view link -->
                        <?php e($html->link('' ,
                            array('action'=>'view', $post['Post']['id']),
                            array(
                                'title'=>__('Click to view the post in another page', true),
                                'class'=>'external')
                        )); ?>
                    </p>
                    <p class="modified">
                        <?php __('Modified on'); ?>&nbsp;
                        <?php echo $html->link($time->format($options['longDateFormat'], $post['Post']['modified']),
                            array('modified' => $time->format($options['queryDateFormat'], $post['Post']['modified'])),
                            array('title' => sprintf(__('View posts modified on %s', true), $time->format($options['queryDateFormat'], $post['Post']['created'])))
                        ); ?><br />
                        <?php __('Modified by'); ?>&nbsp;
                        <?php echo $html->link($post['ModifiedUser']['username'],
                            array('modified_by' => $post['ModifiedUser']['id']),
                            array('title' => sprintf(__('View posts modified by %s', true), $post['ModifiedUser']['username']))
                        ); ?>
                    </p>
                    <!-- Comment actions -->
                    <?php
                        if($post['Post']['published']){
                            $pub_display = 'none';
                            $unpub_display = 'inline';
                        }else{
                            $pub_display = 'inline';
                            $unpub_display = 'none';
                        }
                        if($post['Post']['commentable']){
                            $cmt_display = 'none';
                            $uncmt_display = 'inline';
                        }else{
                            $cmt_display = 'inline';
                            $uncmt_display = 'none';
                        }
                    ?>
                    <p class="actions">
                        <span class="commentable" style="display: <?php e($cmt_display); ?>">
                            <?php echo $html->link(__('Commentable', true),
                                array('action'=>'switchOn', 'commentable', $post['Post']['id']),
                                array('title' => __('allow users to leave comment on this post', true))); ?>
                        </span>
                        <span class="uncommentable" style="display: <?php e($uncmt_display); ?>">
                            <?php echo $html->link(__('UnCommentable', true),
                                array('action'=>'switchOff', 'commentable', $post['Post']['id']),
                                array('title' => __('no comments allowed to this post', true))); ?>
                        </span>
                        <span class="publish" style="display: <?php e($pub_display); ?>">
                            <?php echo $html->link(__('Publish', true),
                                array('action'=>'switchOn', 'published', $post['Post']['id']),
                                array('title' => __('make post visible to students', true))); ?>
                        </span>
                        <span class="unpublish" style="display: <?php e($unpub_display); ?>">
                            <?php echo $html->link(__('Unpublish', true),
                                array('action'=>'switchOff', 'published', $post['Post']['id']),
                                array('title' => __('make post invisible to students', true))); ?>
                        </span>
                        <span class="view">
                            <?php echo $html->link(__('View', true),
                                array('action'=>'view', $post['Post']['id'])); ?>
                        </span>
                        <span class="edit">
                            <?php echo $html->link(__('Edit', true),
                                array('action'=>'edit', $post['Post']['id'])); ?>
                        </span>
                        <span class="delete">
                            <?php echo $html->link(__('Delete', true),
                                array('action'=>'delete', $post['Post']['id'])); ?>
                        </span>
                    </p>
                </td>
                <!-- Post.PostTerm -->
                <td class="category">
                    <p class="category">
                        <?php e($html->link($post['PostTerm']['name'],
                            array('post_term_id'=>$post['Post']['post_term_id']),
                            array('title' => sprintf(__('View posts under category %s', true), $post['PostTerm']['name']))
                        )); ?>
                    </p>
                    <p class="counter">
                        <?php printf(__('%s posts in total', true), $post['PostTerm']['post_count']); ?>
                    </p>
                </td>
                <!-- Post.Comment -->
                <td class="comment">
                    <p class="comment" style="text-align: center;">
                        <?php e($html->link('<span>'.$post['Post']['comment_count'].'</span>',
                            array('controller' =>'comments', 'action'=>'index', 'post_id'=>$post['Post']['id']),
                            array(
                                'title' => sprintf(__('%d comments in total', true), $post['Post']['comment_count']),
                                'class' => 'has_comment'),
                            false, false/* set escape link address to false, as we have html markup in it*/
                        )); ?>
                    </p>
                </td>
                <td class="created">
                    <p class="created">
                        <?php __('Created on'); ?>
                        <?php echo $html->link($time->format($options['longDateFormat'], $post['Post']['created']),
                            array('created' => $time->format($options['queryDateFormat'], $post['Post']['created'])),
                            array('title' => sprintf(__('View posts submitted on %s', true), $time->format($options['queryDateFormat'], $post['Post']['created'])))
                        ); ?>
                    </p>
                    <p class="created">
                        <?php __('Created By'); ?>
                        <?php e($html->link($post['CreatedUser']['username'],
                            array('created_by'=>$post['CreatedUser']['id']),
                            array('title' => sprintf(__('View posts submitted by %s', true), $post['CreatedUser']['username']))
                        )); ?>
                    </p>
                    <p class="clicks">
                        <?php printf(__('Clicks: %d', true), $post['Post']['clicks']); ?>
                    </p>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php else: ?>
    <p><?php __('No Post Found'); ?></p>
<?php endif; ?>

</div>
<?php //debug($posts); ?>
<br class="clear"/>

<script type="text/javascript">
jQuery(function(){
    /* Observe search button click */
    $('a#link_search').click(function(){
        var value = $('#PostSearch').fieldValue()[0];
        if(value != ''){
            this.href = this.href + '/index/title:' + value;
        }else{
            return false;
        }
    });
    /* Show hide action links */
    $('tbody tr').hover($.sme.cbk.showActions, $.sme.cbk.hideActions);
    /* Bind Post Edit/View/Delete callbacks */
    $('table#table_posts p.actions a[href*=switchOn]')
        .click($.sme.cbk.ajaxSwitchOn);
    $('table#table_posts p.actions a[href*=switchOff]')
        .click($.sme.cbk.ajaxSwitchOff);
    $('table#table_posts p.actions a[href*=delete]')
        .click($.sme.cbk.ajaxDelete);
});

</script>